package com.xiaotu.controller;

import com.xiaotu.pojo.Movie;
import com.xiaotu.pojo.MovieType;
import com.xiaotu.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;

@Controller
public class PageController {
    @Autowired
    private MovieService movieService;
    @GetMapping("/")
    public String indexPage(Model model){
        List<MovieType> all = movieService.findAll();
        //System.out.println(all);
        model.addAttribute("movies", all);
        return "index";
    }
    @GetMapping("/login")
    public String loginPage(Model model){
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }
    @GetMapping("/toMoviePage/{type}/{id}")
    public String toMoviePage(
            @PathVariable("type")String type,
            @PathVariable("id")Integer id, Model model){
        Movie byId = movieService.findById(id);
        model.addAttribute("movie", byId);
        return "/movie/"+type+"/movie";
    }
    @GetMapping("/getUserBySession")
    @ResponseBody
    public void getUser(HttpSession httpSession){
        //从当前HttpSession获取绑定到此会话的所有对象名称
        Enumeration<String> attributeNames = httpSession.getAttributeNames();
        while (attributeNames.hasMoreElements()){
            //获取HttpSession中会话名称
            String nextElement = attributeNames.nextElement();
            //获取HttpSession中的应用上下文
            SecurityContextImpl attribute = (SecurityContextImpl) httpSession.getAttribute(nextElement);
            System.out.println("nextElement:   "+nextElement);
            System.out.println("attribute:   "+attribute);
            //获取用户相关信息
            Authentication authentication = attribute.getAuthentication();
            UserDetails principal = (UserDetails) authentication.getPrincipal();
            System.out.println(principal);
            System.out.println("username:   "+principal.getUsername());
        }
    }
    @GetMapping("/getUserByContext")
    @ResponseBody
    public void getUser2(){
        //获取应用上下文
        SecurityContext context = SecurityContextHolder.getContext();
        System.out.println(context);
        //获取用户信息
        Authentication authentication = context.getAuthentication();
        UserDetails principal = (UserDetails)authentication.getPrincipal();
        System.out.println(principal);
        System.out.println("username:   "+principal.getUsername());
    }
}
